﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;

namespace WeChatDataSrc
{
    class MinutesForeData
    {
        static string URL = ConfigurationSettings.AppSettings["MinuteForeURL"];
        /// <summary>
        /// 上传数据
        /// </summary>
        public static void UploadData(DateTime? sd)
        {
            DateTime NowMaxDateTime;
            string MaxDateSendpoint = ReadMax.ReadMaxMinuteFore();
            string SendPoint = string.Empty;
            if (MaxDateSendpoint != "none")
            {
                if (DateTime.TryParse(MaxDateSendpoint, out NowMaxDateTime) == false)
                {
                    NowMaxDateTime = DateTime.Now;
                }
                else
                {
                    NowMaxDateTime.AddMinutes(10);
                }
            }
            else
            {
                NowMaxDateTime = DateTime.Now;
            }
            if (sd.HasValue)
            {
                NowMaxDateTime = Convert.ToDateTime(sd);
            }
            GetData(NowMaxDateTime);
        }

        private static void GetData(DateTime Time)
        {
            DateTime Now = DateTime.Now;
            while (true)
            {
                if (DateTime.Compare(Time, Now) > 0)
                {
                    break;
                }
                StringBuilder sb = new StringBuilder(1024 * 15);
                string Result = string.Format("{0}^", Time.ToString("yyyy-MM-dd HH:mm"));
                sb.Append(Result);
                string ReSendPoint = string.Empty;
                
                string[] Spans = new string[] { "FM00_10", "FM10_20", "FM20_30", "FM30_40", "FM40_50", "FM50_60", "FM60_70", "FM70_80", "FM80_90", "FM90_100", "FM100_110", "FM110_120" };
                foreach (string span in Spans)
                {
                    string sql = string.Format("select * from Minutes_Fore where [date]='{0}' and span='{1}' and semitype='Auto' order by sendpoint desc", Time.ToString("yyyyMMddHHmm"), span);
                    DataTable dt = SqlHelper.ExecuteDataset(SqlHelper.RefineFore_ConnectionString, CommandType.Text, sql).Tables[0];
                    if (dt.Rows.Count > 0)
                    {
                        List<String> FilteredTypes = new List<string>();
                        foreach (DataRow item in dt.Rows)
                        {
                            String Type = item["type"].ToString();
                            String SendPoint = item["sendpoint"].ToString();
                            ReSendPoint = SendPoint;
                            if (FilteredTypes.Contains(Type))
                            {
                                continue;
                            }
                            else //没有添加过该种类型
                            {
                                String Value = item["value"].ToString();
                                var t = string.Format("{0}:{1}:{2}^", span, Type, Value);
                                sb.Append(t);
                                FilteredTypes.Add(Type);
                            }
                        }
                    }
                }
                Time = Time.AddMinutes(10);
                sb.Append(ReSendPoint);
                if (sb.Length > 22)
                {

                    using (System.IO.StreamWriter Writer = new System.IO.StreamWriter("minute.txt",false))
                    {
                        Writer.WriteLine("data=" + sb.ToString().Replace(";", "*"));
                    }
                    HttpPost.SendData(URL, "data=" + sb.ToString().Replace(";", "*"));
                }
            }
        }

        private class CityForeStationModel
        {
            public CityForeStationModel()
            {
                this.StationCode = "";
                this.Data = new CityForeDataModel();
            }
            public string StationCode { get; set; }
            public string Date { get; set; }
            public string Span { get; set; }
            public string SendPoint { get; set; }
            public CityForeDataModel Data { get; set; }
        }

        private class CityForeDataModel
        {
            public CityForeDataModel()
            {
                this.Maxt = "";
                this.Mint = "";
                this.WindDirection = "";
                this.WindSpeed = "";
                this.Rain = "";
            }
            public string Maxt { get; set; }
            public string Mint { get; set; }
            public string WindDirection { get; set; }
            public string WindSpeed { get; set; }
            public string Rain { get; set; }
            public string Curt { get; set; }


        }
    }
}
